using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class TinhTrangRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static TinhTrangDTO CreateTinhTrangDTOFromTinhTrangEF(TinhTrang objEf) // EF -> DTO
        {
            TinhTrangDTO objDTO = new TinhTrangDTO();
            Mapper.CreateMap<TinhTrang, TinhTrangDTO>();
            objDTO = Mapper.Map<TinhTrang, TinhTrangDTO>(objEf);
            return objDTO;
        }

        public static TinhTrang CreateTinhTrangEFFromTinhTrangDTO(TinhTrangDTO objDTO)    //DTO -> EF
        {
            TinhTrang objEF = new TinhTrang();
            Mapper.CreateMap<TinhTrangDTO, TinhTrang>();
            objEF = Mapper.Map<TinhTrangDTO, TinhTrang>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.TinhTrangList
                   select x.MaTinhTrang).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.TinhTrangList
                        orderby x.MaTinhTrang descending
                        select x.MaTinhTrang;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(TinhTrangDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                TinhTrang objEF = CreateTinhTrangEFFromTinhTrangDTO(objDTO);
                context.AddToTinhTrangList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(TinhTrangDTO objDTO)
        {
            try
            {
                TinhTrang objEF = CreateTinhTrangEFFromTinhTrangDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.TinhTrangList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<TinhTrangDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.TinhTrangList
                        orderby x.MaTinhTrang
                        select x).Skip(Start - 1).Take(Record);
            List<TinhTrangDTO> lst = new List<TinhTrangDTO>();
            foreach (var obj in query)
            {
                TinhTrangDTO lp = TinhTrangRepository.CreateTinhTrangDTOFromTinhTrangEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<TinhTrangDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<TinhTrangDTO> lst = new List<TinhTrangDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from TinhTrang in context.TinhTrangList
                        select TinhTrang;
            foreach (var objEf in query)
            {
                TinhTrangDTO objDTO = CreateTinhTrangDTOFromTinhTrangEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public TinhTrangDTO LayTheoMa(int MaTinhTrang)
        {
            TinhTrangDTO objDTO = new TinhTrangDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from TinhTrang in context.TinhTrangList
                            where TinhTrang.MaTinhTrang == MaTinhTrang
                            select TinhTrang;
                TinhTrang objEF = query.First();
                objDTO = CreateTinhTrangDTOFromTinhTrangEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }
        public bool Xoa(int MaTinhTrang, bool XoaKhoaNgoai = false)
        {
            try
            {
                if (XoaKhoaNgoai == true)
                {
                    PhongRepository PhongRepo = new PhongRepository();
                    PhongRepo.XoaTheoMaTinhTrang(MaTinhTrang, XoaKhoaNgoai);
                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.TinhTrangList
                            where x.MaTinhTrang == MaTinhTrang
                            select x;
                if (query.First() != null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        //public bool Xoa(int MaTinhTrang, bool XoaKhoaNgoai=false)
        //{
        //    try
        //    {
        //        if( XoaKhoaNgoai == true)
        //        {
                
        //               PhongRepository PhongRepo = new PhongRepository();
        //               PhongRepo.XoaTheoMaTinhTrang(MaTinhTrang, XoaKhoaNgoai);

        //        }
        //        QuanLyKhachSanEntities context = ContextFactory.GetInstance();
        //        var query = from x in context.TinhTrangList
        //                    where x.MaTinhTrang == MaTinhTrang 
        //                    select x;
        //        if(query.First()!= null)
        //        {
        //            context.DeleteObject(query.First());
        //            context.SaveChanges();
        //        }
        //        return true;
        //    }
        //    catch (System.Exception )
        //    {
        //        return false;
        //    }
        //}

////////////////////////////////////////////////////////////////

    }
}

